
//_______________________________________________________________________________________________________________
/****************************************************************************************************************/
/*                                                                                                              */
/*                     Q U E U E   O F   S I M P L E    M E S S A G E S                                         */
/*                                                                                                              */
/****************************************************************************************************************/

#ifdef OS_ENABLE_QSMSG



    #if defined(OS_QMSG_QSMSG_IDENTICAL)
        // If size of pointer to message eq. to size of simple message then we
        // can use same functions for sending and getting messages.
        #define _OS_SendQSMsg(pQSMsg,SMsg)  _OS_SendQMsg((OST_QMSG*)pQSMsg, (OST_MSG)SMsg)
        #define _OS_IntSendQSMsg(pQSMsg,SMsg)  _OS_IntSendQMsg((OST_QMSG*)pQSMsg, (OST_MSG)SMsg)
        #define _OS_GetQSMsg(pQSMsg)        _OS_GetQMsg((OST_QMSG*)pQSMsg)
    #else
        extern void         _OS_SendQSMsg (OST_QSMSG *pQSMsg, OST_SMSG SMsg);
        extern OST_SMSG     _OS_GetQSMsg (OST_QSMSG *pQSMsg);
    #endif

//------------------------------------------------------------------------------
// Create queue

#define OS_CreateQSMsg(qsmsg, buffer, size)                 \
    {                                                       \
        __OS_QMSG_DI();                                     \
        (qsmsg).Q.cSize = size;                             \
        (qsmsg).Q.cBegin = 0;                               \
        (qsmsg).Q.cFilled = 0;                              \
        (qsmsg).pSMsg = (OST_SMSG*)buffer;                  \
        __OS_QMSG_RI();                                     \
    }

//------------------------------------------------------------------------------
// Check for any message present in queue

#define OS_CheckQSMsg(qsmsg)        ((qsmsg).Q.cFilled)

//------------------------------------------------------------------------------
// Check for queue is full

#define OS_IsQSMsgFull(qsmsg)       ((qsmsg).Q.cFilled == (qsmsg).Q.cSize)

//------------------------------------------------------------------------------
// Check for free room in queue

#define OS_IsQSMsgFree(qsmsg)       ((qsmsg).Q.cFilled ^ (qsmsg).Q.cSize)


//------------------------------------------------------------------------------
// Clear queue

#define OS_ClearQSMsg(qsmsg)        { (qsmsg).Q.cFilled = 0; }

//------------------------------------------------------------------------------
// Send message via queue. If queue full then wait for free place

#define OS_SendQSMsg(qsmsg, value)                                      \
    {                                                                   \
        if (OS_IsQSMsgFull(qsmsg))                                      \
            _OS_WAIT_EVENT(OS_IsQSMsgFree(qsmsg));                      \
        _OS_SendQSMsg(&(qsmsg), value);                                 \
    }                                                                   \

//------------------------------------------------------------------------------
// Send message via queue. If queue full then wait for free place. Exit if timeout expired.

#define OS_SendQSMsgTimeout(qsmsg, value, timeout)                      \
    {                                                                   \
        OS_Flags.bTimeout = 0;                                          \
        if (OS_IsQSMsgFull(qsmsg))                                      \
        {                                                               \
            _OS_WAIT_EVENT_TIMEOUT(OS_IsQSMsgFree(qsmsg), timeout);     \
        }                                                               \
        if (!OS_IsTimeout()) {                                          \
            _OS_SendQSMsg(&(qsmsg), value);                             \
        }                                                               \
    }

//------------------------------------------------------------------------------
// Send message via queue. If queue is full then most rearly message will be pushed out.

#define OS_SendQSMsgNoWait(qsmsg, value)                                \
    {                                                                   \
        _OS_SendQSMsg(&(qsmsg), value);                                 \
    }

//------------------------------------------------------------------------------
// Send message via queue from interrupt. If queue is full then most rearly message will be pushed out.
#if defined(OS_ENABLE_INT_QMSG)
    extern void _OS_IntSendQSMsg (OST_QSMSG *pQSMsg, OST_SMSG SMsg);

    #define OS_IntSendQSMsgNoWait(qsmsg, value)                             \
        {                                                                   \
            _OS_IntSendQSMsg(&(qsmsg), value);                              \
        }
#endif

//------------------------------------------------------------------------------
// Wait message from queue. After accepting message will be deleted from queue.

#define OS_WaitQSMsg(qsmsg, os_smsg_type_var)                           \
    {                                                                   \
        _OS_WAIT_EVENT(OS_CheckQSMsg(qsmsg));                           \
        OS_AcceptQSMsg(qsmsg,os_smsg_type_var);                         \
    }

//------------------------------------------------------------------------------
// Wait message from queue. After accepting message will be deleted from queue. Exit if timeout expired.

#define OS_WaitQSMsgTimeout(qsmsg, os_smsg_type_var, timeout)           \
    {                                                                   \
        _OS_WAIT_EVENT_TIMEOUT(OS_CheckQSMsg(qsmsg), timeout);          \
        if (!OS_IsTimeout())                                            \
        {                                                               \
            OS_AcceptQSMsg(qsmsg,os_smsg_type_var);                     \
        }                                                               \
    }

//------------------------------------------------------------------------------
// 
#define OS_AcceptQSMsg(qsmsg,os_smsg_type_var)                      \
    {                                                               \
        __OS_QMSG_DI();                                             \
        os_smsg_type_var = _OS_GetQSMsg(&(qsmsg));                  \
        __OS_QMSG_RI();                                             \
    }






#endif













